﻿using System;
using System.Web.UI.WebControls;


namespace Carbon.Web.Forms.jQueryForms
{
    public class ListEditor : jQueryFormsViewControlBase
    {
		#region Enums (1)
		public enum PresenterTypeEnum { CheckBox, DropDown, RadioButton };
		#endregion

		#region Fields (2)
		private IListEditorItemCollection m_Items = default(IListEditorItemCollection);

		private bool m_Items_calculated = false;
		#endregion

		#region Properties (2)
		public IListEditorItemCollection Items
        {
            get
            {
                if (!m_Items_calculated)
                {
                    m_Items = (IListEditorItemCollection)ModelValue;
                    m_Items_calculated = true;
                }

                return m_Items;
            }
        }
		protected virtual PresenterTypeEnum PresenterType
        {
            get
            {
                Type propertyType = Binder.Property.PropertyType;
                if (typeof(ICheckBoxListItemCollection).IsAssignableFrom(propertyType))
                {
                    return PresenterTypeEnum.CheckBox;
                }
                else if (typeof(IDropDownListItemCollection).IsAssignableFrom(propertyType))
                {
                    return PresenterTypeEnum.DropDown;
                }
                else if (typeof(IRadioButtonListItemCollection).IsAssignableFrom(propertyType))
                {
                    return PresenterTypeEnum.RadioButton;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }
		#endregion

		#region Methods (3)

		protected override IEditor CreateEditor()
        {
            ListControl dataEditingControl = default(ListControl);

            switch (PresenterType)
            {
                case PresenterTypeEnum.CheckBox:
                    dataEditingControl = new CheckBoxListEditor((ICheckBoxListItemCollection)Items, this) { EnableViewState = false, AutoPostBack = AutoPostback };
                    break;
                case PresenterTypeEnum.DropDown:
                    dataEditingControl = new DropDownListEditor((IDropDownListItemCollection)Items, this) { EnableViewState = false, AutoPostBack = AutoPostback };
                    break;
                case PresenterTypeEnum.RadioButton:
                    dataEditingControl = new RadioButtonListEditor((IRadioButtonListItemCollection)Items, this) { EnableViewState = false, AutoPostBack = AutoPostback };
                    break;
                default:
                    throw new InvalidOperationException();
            }

            return (IEditor)dataEditingControl;
        }

		public override void OnUpdateControl(object sender, UpdateControlEventArgs args)
        {
            m_Items_calculated = false;
            base.OnUpdateControl(sender, args);
        }

		public override void OnUpdateModel(object sender, UpdateModelEventArgs args)
        {
            base.OnUpdateModel(sender, args);
        }

        public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
        {
            RenderBoxes(writer, ErrorDuringUpdateModel,
                ShowErrorText, ErrorTextDuringUpdateModel, () =>
               {
                   base.RenderControl(writer);
               });
        }

        
        private Unit m_Width = default(Unit);
        public Unit Width
        {
            get
            {
                return m_Width;
            }
            set
            {
                m_Width = value;
            }
        } 
		#endregion
    }
}
